function loadScript(url, type) {
  return new Promise((resolve, reject) => {
    var script = document.createElement('script');
    script.onload = resolve;
    script.onerror = reject;
    script.src = url;
    if(type)
    script.type=type
    document.head.appendChild(script);
  });
}

function loadjs(js, type){
    loadScript(js, type)
      .then(() => {
        console.log('Script loaded successfully');
      })
      .catch(error => {
        console.error('Error loading script', error);
      });
}

function initMonaco(elid, vspath, lang, doc, clibs) {
    require.config({ paths: { 'vs': vspath }});
    require(['vs/editor/editor.main'], function() {
        var editor = monaco.editor.create(document.getElementById(elid), {
            value: doc,
            language: lang
        });

        window.monaco_editor = editor

        if(editor && "function" == typeof define && define.amd) {
          delete define.amd;
        }
        clibs.forEach(lib=>{
          console.log(lib)
          loadjs(`lib/${lib}.js`)
        })
    });  
}


[
  // 'link_data', 'menu_data',
].map( js => {loadjs(js+'.js') });


const comps = [
  'counter', 
  'ed/rmed', 'ed/langdd',
  'prompter/opt', 'prompter/form', 'prompter/list', 'prompter/index',
  'test'
]
comps.map( js => {loadjs('comp/'+js+'.js', 'text/babel') });

//pages
const pages = [
  'linkgrid', 'chatbox', 'prompt', 'base_test', 'builder', 'rc_play', 'todo', 'ktree', 'k12train'
]
// pages.map( js => {loadjs('page/'+js+'.js', 'text/babel') });

// loadjs('lib/handlebars.js')

export default {loadjs, initMonaco}